package com.tfswx.jcyw.dmzc.window;

import cn.hutool.core.bean.BeanUtil;
import com.intellij.ide.actions.ShowSettingsUtilImpl;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.tfswx.jcyw.dmzc.api.ApiUtil;
import com.tfswx.jcyw.dmzc.api.dto.LoginInputDTO;
import com.tfswx.jcyw.dmzc.api.dto.LoginOutputDTO;
import com.tfswx.jcyw.dmzc.service.DlxxService;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author ricky
 * @date 2022/10/17
 */
public class DmzcSettingWindow implements Configurable, Configurable.Composite {

    //    private final WebWindow webWindow;
    private final DlxxService dlxx = DlxxService.getInstance();
    private JPanel windowPanel;
    //    private JTextField fwdzTextField;
    private JTextField dlbmTextField;
    private JPasswordField klPasswordField;
    private JLabel rybmLabel;
    private JLabel rymcLabel;
    private JLabel tdmcLabel;
    private JLabel jsbmLabel;
    private JLabel jsmcLabel;
    private JCheckBox djtCheckBox;
    private JCheckBox zjzcCheckBox;
    private JCheckBox zjkfCheckBox;

    public DmzcSettingWindow() {
        setData(dlxx);
    }

    public static void openSetting(Project project) {
        ShowSettingsUtilImpl.getInstance().showSettingsDialog(project, DmzcSettingWindow.class);
    }

    private void login() {
        DlxxService data1 = getData();
//        String fwdz = data1.getFwdz();
        String dlbm = data1.getDlbm();
        String kl = data1.getKl();
        if (StringUtils.isAllBlank(dlbm, kl)) {
            setData(new DlxxService());
            return;
        }
//        if (StringUtils.isBlank(fwdz)) {
//            throw new RuntimeException("服务地址没有填写");
//        }
        if (StringUtils.isBlank(dlbm)) {
            throw new RuntimeException("登录别名没有填写");
        }
        if (StringUtils.isBlank(kl)) {
            throw new RuntimeException("口令没有填写");
        }
        LoginInputDTO loginInputDTO = new LoginInputDTO();
        loginInputDTO.setDlbm(dlbm);
        loginInputDTO.setKl(kl);
        LoginOutputDTO data = ApiUtil.fwdApi.login(loginInputDTO);
        LoginOutputDTO.Ryxx ryxx = data.getRyxx();
        String tdmc = ryxx.getTdmc();
        String jsbm = ryxx.getJsbm();
        String rybm = ryxx.getRybm();
        String rymc = ryxx.getRymc();
        String jsmc = ryxx.getJsmc();
        data1.setDlbm(dlbm);
        data1.setKl(kl);
        data1.setRybm(rybm);
        data1.setRymc(rymc);
        data1.setTdmc(tdmc);
        data1.setJsbm(jsbm);
        data1.setJsmc(jsmc);
        setData(data1);
    }

    @Override
    public String getDisplayName() {
        return "代码走查";
    }

    @Override
    public Configurable @NotNull [] getConfigurables() {
        return new Configurable[0];
    }

    @Override
    public @Nullable JComponent createComponent() {
        return windowPanel;
    }

    @Override
    public boolean isModified() {
        return isModified(dlxx);
    }

    @Override
    public void reset() {
        setData(dlxx);
    }

    @Override
    public void apply() throws ConfigurationException {
        try {
            login();
            DlxxService data = getData();
            BeanUtil.copyProperties(data, dlxx, true);
        } catch (Exception e) {
            throw new ConfigurationException(e.getMessage(), "保存失败");
        }
    }

    public DlxxService getData() {
        DlxxService data = new DlxxService();
//        data.setFwdz(fwdzTextField.getText());
        data.setDlbm(dlbmTextField.getText());
        data.setKl(new String(klPasswordField.getPassword()));

        data.setRybm(rybmLabel.getText());
        data.setRymc(rymcLabel.getText());
        data.setTdmc(tdmcLabel.getText());
        data.setJsbm(jsbmLabel.getText());
        data.setJsmc(jsmcLabel.getText());

        data.setDjtEnable(djtCheckBox.isSelected());
        data.setZjkf(zjkfCheckBox.isSelected());
        data.setZjzc(zjzcCheckBox.isSelected());

        return data;
    }

    public void setData(DlxxService data) {
//        fwdzTextField.setText(data.getFwdz());
        dlbmTextField.setText(data.getDlbm());
        klPasswordField.setText(data.getKl());

        rybmLabel.setText(data.getRybm());
        rymcLabel.setText(data.getRymc());
        tdmcLabel.setText(data.getTdmc());
        jsbmLabel.setText(data.getJsbm());
        jsmcLabel.setText(data.getJsmc());

        djtCheckBox.setSelected(data.isDjtEnable());
        zjkfCheckBox.setSelected(data.isZjkf());
        zjzcCheckBox.setSelected(data.isZjzc());
    }

//    public static void openSetting() {
//        openSetting(null);
//    }

    public boolean isModified(DlxxService data) {
//        String fwdz = fwdzTextField.getText();
//        if (fwdz != null ? !fwdz.equals(data.getFwdz()) : data.getFwdz() != null) {
//            return true;
//        }
        String dlbm = dlbmTextField.getText();
        if (dlbm != null ? !dlbm.equals(data.getDlbm()) : data.getDlbm() != null) {
            return true;
        }
        String kl = new String(klPasswordField.getPassword());
        if (!kl.equals(data.getKl())) {
            return true;
        }
        boolean djtEnable = djtCheckBox.isSelected();
        if (data.isDjtEnable() != djtEnable) {
            return true;
        }
        boolean zjkf = zjkfCheckBox.isSelected();
        if (data.isZjkf() != zjkf) {
            return true;
        }
        boolean zjzc = zjzcCheckBox.isSelected();
        return data.isZjzc() != zjzc;
    }
}
